<script setup>
import TheIcon from "./TheIcon.vue";
import TheAvatar from "./TheAvatar.vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ref } from "vue";

const router = useRouter();

//输入store
const store = useStore();

//搜索功能
const searchPosts = (e) => {
  store.commit("changeSearchTerm", e.target.value);
  router.push({
    path: "/search_result",
    params: {
      term: e.target.valu,
    },
  });
};

//展示头像详情
const showList = ref(false);

//发布帖子
const showUpload = () => {
  store.commit("changeUploadVal", true);
};
</script>

<template>
  <nav class="navbar">
    <!-- 跳转主页 -->
    <router-link to="/"><img src="../assets/logo.svg" /></router-link>
    <!-- 搜索框 -->
    <div class="searchInput">
      <input type="text" @change="searchPosts" />
      <TheIcon icon="search"></TheIcon>
    </div>
    <!-- 用户相关信息 -->
    <div class="navItems">
      <router-link to="/"><TheIcon icon="home"></TheIcon></router-link>
      <button @click="showUpload"><TheIcon icon="publish"></TheIcon></button>
      <div @click="showList = !showList" class="profileDropDown">
        <TheAvatar :width="42" :height="42" style="cursor: pointer"></TheAvatar>
        <div class="dropdownMenu" v-if="showList">
          <ul class="profileMenu">
            <li><router-link to="/profile">个人主页</router-link></li>
            <li>推出登录</li>
          </ul>
        </div>
      </div>
    </div>
  </nav>
</template>

<style scoped>
.navbar {
  width: 100vw;
  height: 80px;
  padding-left: 10vw;
  padding-right: 10vw;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  box-shadow: 0px 0px 24px rgba(0, 0, 0, 0.08);
}

.navbar svg {
  width: 38px;
  height: 38px;
}

.searchInput {
  position: relative;
}

/* .searchInput input {
  width: 100%;
  padding: 12px;
  padding-left: 36px;
  background: #f1f1f1;
  border-radius: 14px;
  border: none;
} */

.searchInput input {
  width: 100%;
  padding: 12px;
  padding-left: 36px;
  background: #f1f1f1;
  border-radius: 14px;
  border: none;
}

.searchInput > svg {
  position: absolute;
  left: 12px;
  top: 11px;
}

.navItems {
  justify-self: end;
  display: flex;
  gap: 24px;
  align-items: center;
}

.navItems > button {
  border: none;
  background: none;
}

.profileDropDown {
  position: relative;
}
.profileMenu {
  position: absolute;
  right: 0;
  width: 116px;
  height: 116px;
  list-style: none;
  background-color: white;
  box-shadow: 0px 0px 24px rgba(0, 0, 0, 0.08);
  border-radius: 4px;
  display: grid;
  /* gap: 18px; */
  text-align: center;
  line-height: 58px;
}

.profileDropDown::before {
  content: "";
  position: absolute;
  display: block;
  width: 0;
  height: 0;
  top: -12px;
  right: 10px;
  border-bottom: 12px solid white;
  border-left: 12px solid transparent;
  border-right: 12px solid transparent;
}

.profileMenu a,
.profileMenu li {
  text-decoration: none;
  cursor: pointer;
}

.profileMenu a:visited {
  color: initial;
}
</style>
